package com.jysz.certificate.persistence.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import com.jysz.certificate.persistence.dao.system.OrganizationDao;
import com.jysz.certificate.persistence.dao.system.UserDao;
import com.jysz.certificate.persistence.model.system.Organization;
import com.jysz.certificate.persistence.model.system.User;
import com.jysz.webcore.ssh.orm.hibernate.HibernateDao;
import com.jysz.webcore.ssh.orm.hibernate.ThreadLocalHolder;

public abstract class AbstractBaseDao<T, PK extends Serializable> extends HibernateDao<T, PK> {
	@Resource
	private OrganizationDao organizationDao; 
	@Resource
	private UserDao userDao; 
	@Autowired 
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;
	
	@Override
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	/**
	 * @return
	 */
	@Override
	public Session getSession() {
		return SessionFactoryUtils.getSession(sessionFactory, true);
	}
	
	public String getShopColumnName(){
		return "shop";
	}
	
	
	/**
	 * 添加权限过滤
	 * @param criteria 查询条件
	 * @param isShopCriteria 是否为shopCriteria
	 * @param user 用户（null代表为当前用户）
	 */
	public void addShopCriteria(Criteria criteria, boolean isShopCriteria,User user){
		if(user==null){
			String userName = ThreadLocalHolder.get().toString();
			user =userDao.getUserByName(userName);
		}
		if(user==null||user.getPost()==null){
			criteria.add(Restrictions.sqlRestriction("1 != 1"));
		}else if(user.getPost().getIndex()!=0){//不是管理员才需要权限过滤
			Long shops[]=getShops(user);
			if(null!=shops&&shops.length>0){
				if(isShopCriteria)
					criteria.add(Restrictions.in("id",shops));
				else{
					criteria.createCriteria(getShopColumnName()).add(Restrictions.in("id",shops));
				}
			}else
				criteria.add(Restrictions.sqlRestriction("1 != 1"));
		}
		
	}
	
	/**
	 * 添加权限过滤
	 * @param criteria 查询条件
	 * @param isShopCriteria 是否为shopCriteria
	 * @param user 用户（null代表为当前用户）
	 */
	public void addShopCriteria(DetachedCriteria criteria, boolean isShopCriteria, User user){
		if(user==null){
			String userName = ThreadLocalHolder.get().toString();
			user =userDao.getUserByName(userName);
		}
		if(user==null||user.getPost()==null){
			criteria.add(Restrictions.sqlRestriction("1 != 1"));
		}else if(user.getPost().getIndex()!=0){//不是管理员才需要权限过滤
			Long shops[]=getShops(user);
			if(null!=shops&&shops.length>0){
				if(isShopCriteria)
					criteria.add(Restrictions.in("id",shops));
				else{
					criteria.createCriteria(getShopColumnName()).add(Restrictions.in("id",shops));
				}
			}else
				criteria.add(Restrictions.sqlRestriction("1 != 1"));
		}
		
	}
	
	/**
	 * 获得当前用户权限范围内的所有店铺ID的数组
	 * @return
	 */
	public Long[] getShops(User user){
		List<Organization> orgList=organizationDao.findShopsByCurrentUser(user);
		if(orgList==null||orgList.size()==0)
			return null;
		Long[]ary=new Long[orgList.size()];
		for(int i=0;i<orgList.size();i++){
			ary[i]=orgList.get(i).getObjectId();
		}
		return ary;
	}
	
}
